var RegisterHandler = function () {
}
RegisterHandler.prototype.listenSendCaptchaEvent = function () {
    var callback = function (event) {
        //将原生js this对象转换为jquery对象
        var $this = $(this);
        event.preventDefault();
        var email = $("input[name='email']").val();
        var reg = /^\w+((.\w+)|(-\w+))@[A-Za-z0-9]+((.|-)[A-Za-z0-9]+).[A-Za-z0-9]+$/;
        if (!email || !reg.test(email)) {
            alert("请输入正确格式的邮箱")
            return;
        }
        myajax.get({
            url: "/email/captcha?email=" + email, success: function (result) {
                if (result['code'] == 200) {
                    //取消发送验证码点击事件
                    $this.off("click");
                    //给按钮添加禁用状态
                    $this.attr("disabled", "disabled")
                    //开始倒计时 设置60s
                    var timercount = 60;
                    //设置定时
                    var interval = setInterval(function () {
                        if (timercount > 0) {
                            //倒计时显示到按钮上
                            $this.text(timercount + "秒后重新发送")
                        } else {
                            $this.text("发送验证码")
                            //取消禁用
                            $this.attr("disabled", false);
                            $this.on("click", callback);
                            //清除倒计时
                            clearInterval(interval);
                        }
                        timercount--;
                    }, 1000);
                } else {
                    alert(result['message'])
                }
            }
        });
    }
    $("#btn_email_captcha").on("click", callback);
}

RegisterHandler.prototype.listenGraphCaptchaEvent = function () {
    $("#captcha_img").on("click", function () {
        var $this = $(this);
        var src = $this.attr("src");
        // 防止一些浏览器，在两次url相同的情况下，不会重新发送请求，导致图形验证码不会更新
        let new_src = myparam.setParam(src, "sign", Math.random());
        $this.attr("src", new_src)
    });
}

RegisterHandler.prototype.listenBtnSubmitEvent = function () {
    $("#btn_submit").on("click", function (event) {
        event.preventDefault();
        var email = $("input[name='email']").val();
        var email_captcha = $("input[name='email_captcha']").val();
        var username = $("input[name='username']").val();
        var password = $("input[name='password']").val();
        var confirm_password = $("input[name='confirm_password']").val();
        var graph_captcha = $("input[name='graph_captcha']").val();
        var reg = /^\w+((.\w+)|(-\w+))@[A-Za-z0-9]+((.|-)[A-Za-z0-9]+).[A-Za-z0-9]+$/;
        //此处简单的填写了下验证,未严格验证
        if (!reg.test(email) || !email || !email_captcha || !username || !password || !confirm_password || !graph_captcha) {
            return;
        }
        myajax.post({
            url: "/register/",
            data: {
                "email": email,
                "email_captcha": email_captcha,
                "username": username,
                "password": password,
                "confirm_password": confirm_password,
                "graph_captcha": graph_captcha
            },
            success: function (result) {
                if (result["code"] == 200) {
                    window.location = "/"
                } else {
                    alert(result["message"]);
                }
            }
        });
    })

}

RegisterHandler.prototype.run = function () {
    this.listenSendCaptchaEvent();
    this.listenGraphCaptchaEvent();
    this.listenBtnSubmitEvent();
}

$(function () {
    var handler = new RegisterHandler();
    handler.run();
})